﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Microsoft.Samples.Office.UnifiedCommunications.Gadget.Config;

namespace Microsoft.Samples.Office.UnifiedCommunications.Gadget
{
    public partial class Top5Config : Page
    {
        private bool loading;

        public Top5Config()
        {
            InitializeComponent();

            this.btnHelp.MouseEnter += new MouseEventHandler(btnHelp_MouseEnter);
            this.btnHelp.MouseLeave += new MouseEventHandler(btnHelp_MouseLeave);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e) { }

        private void CheckTop5Configuration()
        {  
            SettingsTop5 top5Settings = GadgetConfigurationManager.GetUserSettingsTop5();
                chkTagged.IsChecked = top5Settings.Top5UseTagged;
                chkTeamMembership.IsChecked = top5Settings.Top5UseTeamMember;
                chkIMFrequency.IsChecked = top5Settings.Top5UseIM;
                chkCallFrequency.IsChecked = top5Settings.Top5UseCall;
                chkEmailFrequency.IsChecked = top5Settings.Top5UseEmail;
        }

        private void BackDoubleAnimation_Completed(object sender, EventArgs e)
        {
            loading = true;
            CheckTop5Configuration();
            loading = false;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            SettingsWindow.Instance.Close();
        }

        private void btnApply_Click(object sender, RoutedEventArgs e)
        {
            if (!loading)
            {
                GadgetConfigurationManager.SetUserSettingsTop5BySetting(
                    GadgetConfigurationManager.Top5Setting.Tagged, (bool)chkTagged.IsChecked);

                GadgetConfigurationManager.SetUserSettingsTop5BySetting(
                    GadgetConfigurationManager.Top5Setting.TeamMember, (bool)chkTeamMembership.IsChecked);

                GadgetConfigurationManager.SetUserSettingsTop5BySetting(
                    GadgetConfigurationManager.Top5Setting.IM, (bool)chkIMFrequency.IsChecked);

                GadgetConfigurationManager.SetUserSettingsTop5BySetting(
                    GadgetConfigurationManager.Top5Setting.Call, (bool)chkCallFrequency.IsChecked);

                GadgetConfigurationManager.SetUserSettingsTop5BySetting(
                    GadgetConfigurationManager.Top5Setting.Email, (bool)chkEmailFrequency.IsChecked);

                ShowMessage("Your Top 5 configuration has been saved.");
            }
        }

        void btnHelp_MouseLeave(object sender, MouseEventArgs e)
        {
            (base.FindResource("fadeOut") as Storyboard).Begin(this.tooltip);
        }

        void btnHelp_MouseEnter(object sender, MouseEventArgs e)
        {
            (base.FindResource("fadeIn") as Storyboard).Begin(this.tooltip);
        }

        void ShowMessage(string message)
        {
            if (!String.IsNullOrEmpty(message))
            {
                gridMessage.Visibility = Visibility.Visible;
                tbMessage.Text = message;
            }
            else
            {
                tbMessage.Text = string.Empty;
                gridMessage.Visibility = Visibility.Collapsed;
            }
        }
    }
}
